package com.qingcheng.controller.goods;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Spu;
import com.qingcheng.service.goods.SpuService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/spu")
public class SpuController {

    @Reference
    private SpuService spuService;

    @GetMapping("/findAll")
    public List<Spu> findAll() {
        return spuService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Spu> findPage(int page, int size) {
        return spuService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Spu> findList(@RequestBody Map<String, Object> searchMap) {
        return spuService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Spu> findPage(@RequestBody Map<String, Object> searchMap, int page, int size) {
        return spuService.findPage(searchMap, page, size);
    }

    @GetMapping("/findById")
    public Spu findById(String id) {
        return spuService.findById(id);
    }


    @PostMapping("/save")
    public Result save(@RequestBody Goods good) {
        spuService.save(good);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Spu spu) {
        spuService.update(spu);
        return new Result();
    }

    @GetMapping("/delete")
    public Result delete(String id) {
        spuService.delete(id);
        return new Result();
    }

    @GetMapping("/findGoodsById")
    public Goods findGoodsById(String id) {
        return spuService.findGoodsById(id);
    }

    @PostMapping("/audit")
    public Result audit(@RequestBody Map<String, String> auditMap) {
        spuService.audit(auditMap.get("id"), auditMap.get("status"), auditMap.get("message"));
        return new Result();
    }

    @GetMapping("/pull")
    public Result pull(String id) {
        spuService.pull(id);
        return new Result();
    }

    @GetMapping("/put")
    public Result put(String id) {
        spuService.put(id);
        return new Result();
    }

    @GetMapping("/pullMany")
    public Result pullMany(String[] ids) {
        int count = spuService.pullMany(ids);
        return new Result(0, "下架" + count + "个商品");
    }

    @GetMapping("/putMany")
    public Result putMany(String[] ids) {
        int count = spuService.pullMany(ids);
        return new Result(0, "上架" + count + "个商品");
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/createPage")
    public void createPage(String spuid) {
        //将spuid放到消息中间件
        rabbitTemplate.convertAndSend("goodsOnSale", "detail_add", spuid);
        rabbitTemplate.convertAndSend("goodsOnSale", "index_add", spuid);
    }

    @GetMapping("/deletePage")
    public void deletePage(String spuid) {
        //rabbitTemplate.convertAndSend("goodsOffSale","detail_del",spuid);
        rabbitTemplate.convertAndSend("goodsOffSale", "index_del", spuid);
    }

    /**
     * 逻辑删除
     *
     * @param ids
     * @return
     */
    @GetMapping("/logicDel")
    public Result logicDel(String[] ids) {
        int count = spuService.logicDel(ids);
        return new Result(0, "共删除" + count + "个商品");
    }

    /**
     * 还原
     *
     * @param ids
     * @return
     */
    @GetMapping("/reduction")
    public Result reduction(String[] ids) {
        int count = spuService.reduction(ids);
        return new Result(0, "共还原" + count + "个商品");
    }

    /**
     * 物理删除
     *
     * @param ids
     * @return
     */
    @GetMapping("/physicalDel")
    public Result physicalDel(String[] ids) {
        int count = spuService.physicalDel(ids);
        return new Result(0, "共物理删除" + count + "个商品");
    }
}
